﻿'
' Boogaloo® - http://www.mindfly.com
' Copyright (c) 2008
' by Mindfly, Inc.
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
' INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
' DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
' SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
' SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
' WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
' THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Imports Boogaloo.Core
Imports Boogaloo.Core.UI
Imports Boogaloo.Core.BLL

Namespace UI.UserControls


    Partial Public Class Site_TreeScrollerChildTree
        Inherits AdminNavControlBase

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

            ' need a check to figure out if this is in the path of 
            ' the next level down in the case of child list linkbutton click
            BindControl()


        End Sub

        Private Sub BindControl()

            If ref_node IsNot Nothing Then
                Dim child_sort_type_id As Integer = 0

                If ref_node.parent_id = 0 Then
                    child_sort_type_id = ChildSortType.ManualNodesInsertedLast
                Else
                    child_sort_type_id = ref_node.parent_node.child_sort_type_id
                End If

                ' Add these back in when list is to be paged
                ' -----------------------------------------------------
                'Dim page_size As Integer = CType(CoreSettings.GetSetting("ADMIN_TreeScrollerPageSize"), Integer)
                'Dim page_index As Integer = GetPageIndexToQuery(page_size)
                '------------------------------------------------------
                Dim children As List(Of Node) = Node.GetChildNodes(Groove.admin_root.root_id, ref_node.parent_id, child_sort_type_id)

                rpt.DataSource = children
                rpt.DataBind()
            Else
                Me.Visible = False
            End If

        End Sub


        Protected Sub rpt_ItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs) Handles rpt.ItemDataBound

            Dim n As Node = CType(e.Item.DataItem, Node)
            If n IsNot Nothing Then

                Dim spn_icon As HtmlGenericControl = CType(e.Item.FindControl("spn_icon"), HtmlGenericControl)
                Dim nodisplay As String = String.Empty
                If n.display = False Then nodisplay = " nodisplay"
                Select Case n.node_type_id
                    Case NodeType.WebPage
                        spn_icon.InnerHtml = "Webpage"
                        spn_icon.Attributes.Add("class", String.Format("webpageNode{0}", nodisplay))
                    Case NodeType.Folder
                        spn_icon.InnerHtml = "Folder"
                        spn_icon.Attributes.Add("class", String.Format("folderNode{0}", nodisplay))
                    Case NodeType.ExternalUrl
                        spn_icon.InnerHtml = "External Url"
                        spn_icon.Attributes.Add("class", String.Format("externalUrlNode{0}", nodisplay))
                    Case NodeType.LocalFile
                        spn_icon.InnerHtml = "Local File"
                        spn_icon.Attributes.Add("class", String.Format("localFileNode{0}", nodisplay))
                End Select

                Dim hyp_node As HyperLink = CType(e.Item.FindControl("hyp_node"), HyperLink)
                hyp_node.NavigateUrl = GetGoToNodeUrl(n.node_id)

                Dim hyp_text As String = n.menu_item_text
                If hyp_text.Length > 22 Then hyp_text = hyp_text.Substring(0, 20) & "..."

                hyp_node.Text = hyp_text

                Dim lb_children As LinkButton = CType(e.Item.FindControl("lb_children"), LinkButton)
                If n.has_children Then
                    lb_children.Visible = True
                    lb_children.ID = "lbexpand" & n.child_nodes(0).node_id.ToString
                Else
                    lb_children.Visible = False
                End If

                Dim ref As Node = ExpandTree(n)
                If ref IsNot Nothing Then
                    CType(e.Item.FindControl("li"), HtmlGenericControl).Attributes.Add("class", "selected")
                 ElseIf n.node_id = current_node.node_id Then
                    CType(e.Item.FindControl("li"), HtmlGenericControl).Attributes.Add("class", "selected")
                End If

            End If

        End Sub




        Protected Function ExpandTree(ByVal n As Node) As Node
            Dim found As Node = Nothing
            Dim i As Integer = 0
            While i < breadcrumb.Count And found Is Nothing
                If breadcrumb(i) Is Nothing Then Exit While
                If breadcrumb(i).parent_id = n.node_id Then
                    found = breadcrumb(i)
                Else
                    i += 1
                End If
            End While
            Return found
        End Function

        Protected Sub lb_children_Click(ByVal sender As Object, ByVal e As EventArgs)
            BindControl()
        End Sub


        Private Function GetPageIndexToQuery(ByVal page_size As Integer) As Integer
            Return ref_node.sort_order / page_size
        End Function



        Private _ref_node As Node
        Public Property ref_node() As Node
            Get
                Return _ref_node
            End Get
            Set(ByVal value As Node)
                _ref_node = value
            End Set
        End Property



        Public ReadOnly Property current_node() As Node
            Get
                If Groove.admin_current_node Is Nothing Then
                    If Groove.admin_root.default_node Is Nothing Then
                        Return Nothing
                    Else
                        Return Groove.admin_root.default_node
                    End If
                Else
                    Return Groove.admin_current_node
                End If
            End Get
        End Property
  


    End Class

End Namespace